package com.phkj.qdh.ui.dialog

import android.content.Context
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hjq.shape.view.ShapeButton
import com.hjq.toast.ToastUtils
import com.lxj.xpopup.core.CenterPopupView
import com.phkj.qdh.R
import com.phkj.qdh.bean.BaseInspectBean
import com.phkj.qdh.bean.Child
import com.phkj.qdh.ui.adapter.basetreenode.BaseSortContentNode
import com.phkj.qdh.ui.adapter.basetreenode.BaseSortHeadNode
import com.phkj.qdh.ui.adapter.basetreenode.BaseTreeNodeAdapter
import com.phkj.qdh.utils.MyItemDecoration
import com.phkj.qdh.utils.node.BaseNode

class BaseInspectTreeSelectDialog(context: Context, val list: ArrayList<BaseInspectBean>) :
    CenterPopupView(context) {
    private var title: String = ""
    private val baseTreeNodeAdapter by lazy { BaseTreeNodeAdapter(this) }
    val nodeList = ArrayList<BaseNode>()
    fun setTitle(s: String) {
        this.title = s
    }

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_select_man
    }

    override fun onCreate() {
        super.onCreate()
        val tvTitle = findViewById<TextView>(R.id.tv_title)
        val rv = findViewById<RecyclerView>(R.id.rv_item)
        rv.run {
            layoutManager = LinearLayoutManager(context)
            addItemDecoration(MyItemDecoration(width = 5))
            adapter = baseTreeNodeAdapter
        }
        tvTitle.text = title


        list.forEachIndexed { index1, child1 ->
            val contentList = ArrayList<BaseNode>()
            child1.childrenList?.forEachIndexed { index2, child ->
                val content = BaseSortContentNode(index1, index2, child)
                contentList.add(content)
            }
            val head = BaseSortHeadNode(index1, child1, contentList)
            nodeList.add(head)
        }
        baseTreeNodeAdapter.setList(nodeList)

        findViewById<ShapeButton>(R.id.btn_confirm).setOnClickListener {
            var data: Pair<BaseInspectBean?, BaseInspectBean?>? = null
            nodeList.forEach { head ->
                head as BaseSortHeadNode
                if (head.isSelected) {
                    data = Pair(head.head, null)
                    return@forEach
                }
                head.childNode?.forEach { child ->
                    child as BaseSortContentNode
                    if (child.isSelected) {
                        data = Pair(head.head, child.content)
                        return@forEach
                    }
                }
            }

            if (data != null) {
                listener?.ok(data!!)
                dismiss()
            } else {
                ToastUtils.show("请选择$title")
            }

        }
    }

    private var listener: SelectBaseSortListener? = null
    fun setSelectSortListener(l: SelectBaseSortListener) {
        listener = l
    }
}

interface SelectBaseSortListener {
    fun ok(data:Pair<BaseInspectBean?, BaseInspectBean?>)
}

